package com.example.hibernate.core;

import java.util.List;
import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.example.hibernate.pojos.ITableObject;
import com.example.hibernate.pojos.UserOperation;
import com.example.hibernate.pojos.Users;
import com.example.hibernate.utils.QueryBuilder;
import com.example.utils.Constants;
import com.example.utils.ResourceLoader;

public class HibernateCommunicator {

	private static HibernateCommunicator _instance = null;
	private SessionFactory sessionFactory = null;

	private HibernateCommunicator() {
		try {
			Configuration cfg = new Configuration();
			Properties properties = new Properties();
			properties.load(ResourceLoader.getInstance()
					.getInputStreamResource(
							"conf/hibernate.properties",
							System.getProperty("confLocation",
									Constants.EMPTY_STRING)));
			cfg.setProperties(properties);
//			cfg.setProperty(Environment.DRIVER, "com.ibm.db2.jcc.DB2Driver");
//			cfg.setProperty(Environment.URL, "jdbc:db2://localhost:50000/xadb");
//			cfg.setProperty(Environment.USER, "db2admin");
//			cfg.setProperty(Environment.PASS, "db2@dmin");
//			cfg.setProperty(Environment.DIALECT, DB2Dialect.class.getName());
//			cfg.setProperty(Environment.SHOW_SQL, "true");
////			cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
//			cfg.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
//			cfg.setProperty(Environment.AUTO_CLOSE_SESSION, "true");
//			cfg.setProperty(Environment.AUTOCOMMIT, "true");
//			cfg.setProperty(Environment.DEFAULT_SCHEMA, "FUNFUSION");

			cfg.addClass(Users.class);
			cfg.addClass(UserOperation.class);

			sessionFactory = cfg.buildSessionFactory();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void closeDown(){
		sessionFactory.getCurrentSession().close();
		sessionFactory.close();
	}

	public void insertNewRecord(ITableObject myObject){
		Transaction t = sessionFactory.getCurrentSession().getTransaction();
		t.begin();
		sessionFactory.getCurrentSession().save(myObject);
		t.commit();
//		sessionFactory.getCurrentSession().flush();
	}
	
	public ITableObject readSingleRecord(String whereClause, List<Object> params) {
		List l = readMultipleRecord(whereClause, params);
		if (l.size() > 0) {
			ITableObject iTableObject = (ITableObject) l.get(0);
			return iTableObject;
		}
		return null;
	}
	
	public Object readSingleRecordGeneric(String whereClause, List<Object> params) {
		List l = readMultipleRecord(whereClause, params);
		if (l.size() > 0) {
			return l.get(0);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List readMultipleRecord(String whereClause, List<Object> params){
		sessionFactory.getCurrentSession().beginTransaction();
//		sessionFactory.getCurrentSession().merge(arg0)
		List l = sessionFactory.getCurrentSession().createQuery(QueryBuilder.buildWhereClause(whereClause, params)).list();
		return l;
	}

	public static HibernateCommunicator getInstance() {
		if (_instance == null) {
			_instance = new HibernateCommunicator();
		}
		return _instance;
	}
	
//	public static void main(String[] args) {
//		UserOperation operation = new UserOperation();
//		operation.setFfid(100);
//		operation.setLoginStatus(IUserOperation.LOGGEDOUT);
//		operation.setTimeStamp(new Timestamp(new Date().getTime()));
//		operation.setUserName("Testing");
//		HibernateCommunicator.getInstance().insertNewRecord(operation);
//		Users users = new Users();
//		users.setFfid(100);
//		users.setUserName("tumstech");
//		users.setPassword("Tumstech1");
//		HibernateCommunicator.getInstance().addData(users);
		
//		List l = new ArrayList();
//		l.add("tumstech");
//		Users myObject = (Users) HibernateCommunicator.getInstance().readSingleRecord(AllWhereClause.GET_USER_DETAILS_FROM_USERNAME, l);
//		System.out.println(myObject);
		
//		users = new Users();
//		users.setId(2);
//		users.setUserName("tumstech");
//		users.setPassword("Tumstech1");
//		HibernateCommunicator.getInstance().addData(users);
//		users = new Users();
//		users.setId(3);
//		users.setUserName("tumstech");
//		users.setPassword("Tumstech1");
//		HibernateCommunicator.getInstance().addData(users);
//	}
}
